package com.spareparts.protocol;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.spareparts.entyties.JuDian;
import com.spareparts.entyties.SearchResponseModel;
import com.spareparts.entyties.SpareType;
import com.spareparts.entyties.Temp;
import com.spareparts.entyties.WareHouse;
import com.spareparts.entyties.WuLiao;

import cm.framework.protocol.BaseJSONRsponse;

public class SearchResponse extends BaseJSONRsponse {
	public SearchResponseModel searchResponseModel ;
	@Override
	protected boolean extractBody(JSONObject arg0) {
		searchResponseModel = new SearchResponseModel();
		searchResponseModel.temp = new Temp();
		searchResponseModel.wuLiaoList = new ArrayList<WuLiao>();
		try {
			JSONObject tempJO = arg0.getJSONObject("res").getJSONObject("temp");
			if(tempJO!=null){
				searchResponseModel.temp.numbersid = getJSONStringValue("numbersid",tempJO);
				searchResponseModel.temp.number = getJSONStringValue("number",tempJO);
				searchResponseModel.temp.numberwid = getJSONStringValue("numberwid",tempJO);
				searchResponseModel.temp.count = tempJO.has("count")?tempJO.getInt("count"):0;
				searchResponseModel.temp.numberrid = getJSONStringValue("numberrid",tempJO);
			}
			JSONArray dataJsonArray = arg0.getJSONObject("res").getJSONArray("list");
			if (dataJsonArray != null && dataJsonArray.length() > 0) {
				WuLiao wuLiao = null;
				JSONObject wuliaoJO = null;
				for (int i = 0; i < dataJsonArray.length(); i++) {
					wuLiao = new WuLiao();
					wuliaoJO = dataJsonArray.getJSONObject(i);
					wuLiao.id = getJSONStringValue("id",wuliaoJO);
					wuLiao.sid = getJSONStringValue("sid",wuliaoJO);
					wuLiao.veneercode = getJSONStringValue("veneercode",wuliaoJO);
					wuLiao.number = getJSONStringValue("number",wuliaoJO);
					wuLiao.wid = getJSONStringValue("wid",wuliaoJO);
					wuLiao.sparetype = getJSONSpareTypeValue(wuliaoJO);
					wuLiao.warehouse = getJSONWareHouseValue(wuliaoJO);
					searchResponseModel.wuLiaoList .add(wuLiao);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return true;
	}
	/**
	 * 获取json中的String
	 * @param name
	 * @param jo
	 * @return
	 */
	private String getJSONStringValue(String name, JSONObject jo){
		String value = null;
		if(jo.has(name)){
			try {
				value = jo.getString(name);
			} catch (JSONException e) {
				value = null;
				e.printStackTrace();
			}
		}
		return value;
	}

	private SpareType getJSONSpareTypeValue(JSONObject jo){
		SpareType spareType = null;
		if(jo.has("sparetype")){
			spareType = new SpareType();
			try {
				jo = jo.getJSONObject("sparetype");
				spareType.code =getJSONStringValue("code",jo);
				spareType.descrption = getJSONStringValue("descrption",jo);
				spareType.productline =  getJSONStringValue("productline",jo);
				spareType.name = getJSONStringValue("name",jo);
			} catch (JSONException e) {
				spareType = null;
				e.printStackTrace();
			}
		}
		return spareType;
	}
	
	private WareHouse getJSONWareHouseValue(JSONObject jo){
		WareHouse wareHouse = null;
		if(jo.has("warehouse")){
			wareHouse = new WareHouse();
			try {
				jo = jo.getJSONObject("warehouse");
				wareHouse.id =getJSONStringValue("id",jo);
				wareHouse.name = getJSONStringValue("name",jo);
				wareHouse.pointname =  getJSONStringValue("pointname",jo);
				wareHouse.rid = getJSONStringValue("rid",jo);
				wareHouse.headman = getJSONStringValue("headman",jo);
				wareHouse.contact = getJSONStringValue("contact",jo);
				wareHouse.address = getJSONStringValue("address",jo);
				wareHouse.sort = getJSONStringValue("sort",jo);
				wareHouse.note = getJSONStringValue("note",jo);
			} catch (JSONException e) {
				wareHouse = null;
				e.printStackTrace();
			}
		}
		return wareHouse;
	}
	
}
